#include "stdafx.h"
#include "config.h"
#include "provider.h"


// {284ECEF0-AF6E-41e5-BE9F-87870B9128C5}
static const GUID guid_lyrics_grabber = 
{ 0x284ecef0, 0xaf6e, 0x41e5, { 0xbe, 0x9f, 0x87, 0x87, 0xb, 0x91, 0x28, 0xc5 } };
FOOGUIDDECL cfg_lyrics_grabber g_cfg_lyrics_grabber(guid_lyrics_grabber);

void lyrics_grabber_config_item::set_default_value()
{
	preferred_provider = lyric_provider_lyricwiki::provider_guid;
	lyric_filed_name = "LYRICS";
	skip_tag_present = true;
	proxy_type = PROXY_NONE;
	proxy_hostport = "";
	proxy_need_auth = false;
	proxy_username = "";
	proxy_password = "";
}

// save
void cfg_lyrics_grabber::get_data_raw(stream_writer * p_stream,abort_callback & p_abort)
{
	try
	{
		// Write version
		p_stream->write_lendian_t(VERSION_CURRENT, p_abort);
		// Others
		p_stream->write_lendian_t(m_val.skip_tag_present, p_abort);
		p_stream->write_lendian_t(m_val.preferred_provider, p_abort);
		p_stream->write_string(m_val.lyric_filed_name, p_abort);
		p_stream->write_lendian_t(m_val.proxy_type, p_abort);
		p_stream->write_string(m_val.proxy_hostport, p_abort);
		p_stream->write_lendian_t(m_val.proxy_need_auth, p_abort);
		p_stream->write_string(m_val.proxy_username, p_abort);
		p_stream->write_string(m_val.proxy_password, p_abort);
	}
	catch (...)
	{
		return;
	}
}

// load
void cfg_lyrics_grabber::set_data_raw(stream_reader * p_stream,t_size p_sizehint,abort_callback & p_abort)
{
	t_uint32 read_version;

	// fisrt set all values to defaults
	m_val.set_default_value();

	if (p_sizehint < sizeof(t_uint32))
		return;

	try
	{
		// Read version from stream
		p_stream->read_lendian_t(read_version, p_abort);

		switch (read_version)
		{
		case VERSION_CURRENT:
			p_stream->read_lendian_t(m_val.skip_tag_present, p_abort);

		case VERSION_0107:
			p_stream->read_lendian_t(m_val.preferred_provider, p_abort);
			p_stream->read_string(m_val.lyric_filed_name, p_abort);
			p_stream->read_lendian_t(m_val.proxy_type, p_abort);
			p_stream->read_string(m_val.proxy_hostport, p_abort);
			p_stream->read_lendian_t(m_val.proxy_need_auth, p_abort);
			p_stream->read_string(m_val.proxy_username, p_abort);
			p_stream->read_string(m_val.proxy_password, p_abort);
			break;

		default:
			// Reset
			m_val.set_default_value();
			return;
			break;
		}
	}
	catch (...)
	{
		m_val.set_default_value();
		return;
	}
}
